package com.hulk.dryad.manage.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hulk.dryad.common.constant.CommonConstants;
import com.hulk.dryad.persistence.injector.datascope.DataScope;
import com.hulk.dryad.persistence.entity.SysDeptModel;
import com.hulk.dryad.persistence.entity.SysDeptRelationModel;
import com.hulk.dryad.persistence.entity.tools.DeptTree;
import com.hulk.dryad.persistence.entity.tools.TreeUtil;
import com.hulk.dryad.persistence.mapper.SysDeptMapper;
import com.hulk.dryad.manage.provider.service.SysDeptRelationService;
import com.hulk.dryad.manage.provider.service.SysDeptService;

import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 * @author kim
 * @since 2018-01-20
 */
@Service
@DS("#" + CommonConstants.TENANT_ID)
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDeptModel> implements SysDeptService {

	private final SysDeptRelationService sysDeptRelationService;


	/**
	 * 添加信息部门
	 * @param dept 部门
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveDept(SysDeptModel dept) {
		SysDeptModel sysDeptModel = new SysDeptModel();
		BeanUtils.copyProperties(dept, sysDeptModel);
		this.save(sysDeptModel);
		sysDeptRelationService.insertDeptRelation(sysDeptModel);
		return Boolean.TRUE;
	}

	/**
	 * 删除部门
	 * @param id 部门 ID
	 * @return 成功、失败
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeDeptById(Integer id) {
		// 级联删除部门
		List<Integer> idList = sysDeptRelationService
				.list(Wrappers.<SysDeptRelationModel>query().lambda().eq(SysDeptRelationModel::getAncestor, id)).stream()
				.map(SysDeptRelationModel::getDescendant).collect(Collectors.toList());

		if (CollUtil.isNotEmpty(idList)) {
			this.removeByIds(idList);
		}

		// 删除部门级联关系
		sysDeptRelationService.deleteAllDeptRealtion(id);
		return Boolean.TRUE;
	}

	/**
	 * 更新部门
	 * @param sysDeptModel 部门信息
	 * @return 成功、失败
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean updateDeptById(SysDeptModel sysDeptModel) {
		// 更新部门状态
		this.updateById(sysDeptModel);
		// 更新部门关系
		SysDeptRelationModel relation = new SysDeptRelationModel();
		relation.setAncestor(sysDeptModel.getParentId());
		relation.setDescendant(sysDeptModel.getDeptId());
		sysDeptRelationService.updateDeptRealtion(relation);
		return Boolean.TRUE;
	}

	/**
	 * 查询全部部门树
	 * @return 树
	 */
	@Override
	public List<DeptTree> selectTree() {
		// 查询全部部门
		List<SysDeptModel> deptAllList = baseMapper.selectList(Wrappers.emptyWrapper());
		// 查询数据权限内部门
		List<Integer> deptOwnIdList = baseMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
				.map(SysDeptModel::getDeptId).collect(Collectors.toList());

		// 权限内部门
		List<DeptTree> collect = deptAllList.stream().filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
				.sorted(Comparator.comparingInt(SysDeptModel::getSort)).map(dept -> {
					DeptTree node = new DeptTree();
					node.setId(dept.getDeptId());
					node.setParentId(dept.getParentId());
					node.setName(dept.getName());

					// 有权限不返回标识
					if (deptOwnIdList.contains(dept.getDeptId())) {
						node.setIsLock(Boolean.FALSE);
					}
					return node;
				}).collect(Collectors.toList());
		return TreeUtil.build(collect, 0);
	}

	@Override
	public List<Integer> selectScope() {
		List<Integer> deptOwnIdList = baseMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
				.map(SysDeptModel::getDeptId).collect(Collectors.toList());
		return deptOwnIdList;
	}
}
